package cfgs.gestudent.dao.grup;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import cfgs.gestudent.dao.connexio.ConnexioDB;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;

public class ObtenirGrup {
	
	private ConfiguracionsIdiomes idioma = new ConfiguracionsIdiomes();
	private ResourceBundle rb = idioma.getIdioma();
	
	private ArrayList<String> llista = null;
	private ArrayList<String> llistaId = null;
	
	
	public ArrayList<String> getLlista() {
		return llista;
	}



	public void setLlista(ArrayList<String> llista) {
		this.llista = llista;
	}



	public ArrayList<String> getLlistaId() {
		return llistaId;
	}
	
	public int getLlistaIdIndex(int index){
		return Integer.parseInt(llistaId.get(index));
	}



	public void setLlistaId(ArrayList<String> llistaId) {
		this.llistaId = llistaId;
	}



	public ArrayList<String> llistesComboBoxId(int curs) {
		llista = new ArrayList<>();
		llistaId = new ArrayList<>();

		Connection connexio = ConnexioDB.getConnexio();
		ResultSet rs = null;
		Statement st = null;

		String query = "SELECT gc.Id AS Id, gc.Nom AS Nom FROM CURS c, GRUP_CURS gc WHERE c.Id="+curs+" AND c.id=gc.Id_Curs";
		
		try {
			st = connexio.createStatement();
			rs = st.executeQuery(query);
			while (rs.next()) {
				String grup = rs.getString("Nom");
				String id = rs.getString("Id");
				llista.add(grup);
				llistaId.add(id);
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null,
					rb.getString("missatgeErrorStatement") + "\n " + e,
					rb.getString("titolErrorStatement"),
					JOptionPane.ERROR_MESSAGE);
		} finally {
			try {
				connexio.close();
				st.close();
				rs.close();
			} catch (SQLException e) {
				JOptionPane
						.showMessageDialog(null,
								rb.getString("missatgeErrorTancarConnexio")
										+ "\n " + e,
								rb.getString("titolErrorTancarConnexio"),
								JOptionPane.ERROR_MESSAGE);
			}
		}

		return llista;
	}
	
	public ArrayList<String> llistesComboBox(int curs) {
		llista = new ArrayList<>();
		llistaId = new ArrayList<>();

		Connection connexio = ConnexioDB.getConnexio();
		ResultSet rs = null;
		Statement st = null;

		String query = "SELECT gc.Id AS Id, gc.Nom AS Nom FROM CURS c, GRUP_CURS gc WHERE c.Id='"+curs+"' AND c.id=gc.Id_Curs";
		
		try {
			st = connexio.createStatement();
			rs = st.executeQuery(query);
			while (rs.next()) {
				String grup = rs.getString("Nom");
				String id = rs.getString("Id");
				llista.add(grup);
				llistaId.add(id);
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null,
					rb.getString("missatgeErrorStatement") + "\n " + e,
					rb.getString("titolErrorStatement"),
					JOptionPane.ERROR_MESSAGE);
		} finally {
			try {
				connexio.close();
				st.close();
				rs.close();
			} catch (SQLException e) {
				JOptionPane
						.showMessageDialog(null,
								rb.getString("missatgeErrorTancarConnexio")
										+ "\n " + e,
								rb.getString("titolErrorTancarConnexio"),
								JOptionPane.ERROR_MESSAGE);
			}
		}

		return llista;
	}

}
